from .SumaMain import *
from .SumaLog import *

class SumaHTTP:
    def __init__(self):
        self.creater_name = "SumaRoder"
        self.creater_qq = "1493813167"
        self.function_name = "SumaHTTP"
        self.function_desc = "This is a HTTP send function, it based on requests."
        self.function_version = "1.7.9"
        # 一条没什么用的分界线 awa
        self.exec_url = ""
        self.exec_headers = {"Connection":"keep-alive","User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0","Content-Type":"Application/Json"}
        self.exec_method = "get"
        self.exec_param = None
        self.exec_timeout = 40
        self.readyRequest = False
    def OpenHTTPConnection(self,url,headers={"Connection":"keep-alive","User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0","Content-Type":"Application/Json"},method="get",param=None,timeout=40):
        try:
            self.exec_url = url
            self.exec_headers = headers
            self.exec_method = method
            self.exec_param = param
            self.exec_timeout = timeout
            self.readyRequest = True
        except Exception as e:
            SumaLog.log("",type="error",e=e)
            traceback.print_exc(file=sys.stderr)
    def SendHTTPConnection(self,connectNum=4,responseType="text"):
        if not self.readyRequest:
            return None
        try:
            if not self.exec_url or not connectNum or connectNum <= 2:
                return None
            connectNum -= 1
            redo = 1
            try:
                if self.exec_method == "post":
                    response = requests.post(self.exec_url,timeout=self.exec_timeout,headers=self.exec_headers,param=self.exec_param)
                    if response.status_code == 200:
                        if responseType == "content":
                            return response.content
                        else:
                            return response.text
                    else:
                        redo += 1
                else:
                    response = requests.get(self.exec_url,timeout=self.exec_timeout,headers=self.exec_headers)
                    if response.status_code == 200:
                        if responseType == "content":
                            return response.content
                        else:
                            return response.text
                    else:
                        redo += 1
            except requests.exceptions.Timeout:
                # 请求超时
                redo += 1
            except requests.exceptions.RequestException:
                redo += 1
            for i in range(connectNum+2):
                # time.sleep(0.2)
                if redo-2 == connectNum:
                    SumaLog.log("所有测试均已连接失败，共访问"+str(connectNum+1)+"次",group="REQUESTER")
                    return False
                SumaLog.log(" 开始第"+str(redo)+"次重试",group="REQUESTER")
                try:
                    if self.exec_method == "post":
                        response = requests.post(self.exec_url,timeout=self.exec_timeout,headers=self.exec_headers,param=self.exec_param)
                        if response.status_code == 200:
                            if responseType == "content":
                                return response.content
                            else:
                                return response.text
                        else:
                            SumaConsole.clearLastLine()
                            redo += 1
                            continue
                    else:
                        response = requests.get(self.exec_url,timeout=self.exec_timeout,headers=self.exec_headers)
                        if response.status_code == 200:
                            if responseType == "content":
                                return response.content
                            else:
                                return response.text
                        else:
                            SumaConsole.clearLastLine()
                            redo += 1
                            continue
                except requests.exceptions.Timeout:
                    # 请求超时
                    SumaConsole.clearLastLine()
                    redo += 1
                    continue
                except requests.exceptions.RequestException:
                    SumaConsole.clearLastLine()
                    redo += 1
                    continue
        except Exception as e:
            SumaLog.log("",type="error",e=e)
            traceback.print_exc(file=sys.stderr)
    def CloseHTTPConnetion(self,clearTmp=False):
        try:
            self.exec_url = ""
            self.exec_headers = {"Connection":"keep-alive","User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0","Content-Type":"Application/Json"}
            self.exec_method = "get"
            self.exec_param = None
            self.exec_timeout = 40
            self.readyRequest = False
            if clearTmp:
                response = None
                redo = None
                connectNum = None
        except Exception as e:
            SumaLog.log("",type="error",e=e)
            traceback.print_exc(file=sys.stderr) 
SumaHTTP = SumaHTTP()
